package com.lnsf.bookstore.web.scan.controller;

import com.lnsf.bookstore.common.annotation.IgnoreAuth;
import com.lnsf.bookstore.common.annotation.LoginUser;
import com.lnsf.bookstore.common.controller.ApiBaseAction;
import com.lnsf.bookstore.common.util.DateUtil;
import com.lnsf.bookstore.web.Auth.controller.ApiAuthController;
import com.lnsf.bookstore.web.book.service.ApiBookService;
import com.lnsf.bookstore.web.book.vo.ApiBook;
import com.lnsf.bookstore.web.borrow.service.ApiBorrowService;
import com.lnsf.bookstore.web.borrow.vo.ApiBorrow;
import com.lnsf.bookstore.web.user.vo.ApiUser;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/scan")
public class ApiScanController extends ApiBaseAction{

    @Autowired
    private ApiBorrowService apiBorrowService;

    @Autowired
    private ApiBookService apiBookService;

    private Logger logger = Logger.getLogger(ApiScanController.class);

    @RequestMapping("/borrow-book")
    public Object borrowBook(@LoginUser ApiUser apiUser,String isbn){
        //测试
        /*apiUser = new ApiUser();
        apiUser.setCredit(81);
        apiUser.setUserId(31);*/

        Map<String,Object> resultObject = new HashMap<String, Object>();
        Map params = new HashMap();
        params.put("isbn",isbn);
        ApiBook apiBook = apiBookService.checkBook(params);
        if(apiBook == null ){
            return toResponsFail("扫描失败，该图书未录入数据库或不存在该图书，请联系前台或者管理员");
        }
        if(apiUser.getCredit()<80){
            return toResponsFail("借书失败,您的用户信用值低于80，请保持良好的借阅习惯");
        }
        if(apiBook.getBroNum() == 0 ){
            return toResponsFail("借书失败! 该图书可借阅量已经满了");
        }
        Map totalParam = new HashMap();
        totalParam.put("userId",apiUser.getUserId());
        int borrowNum = apiBorrowService.queryTotal(totalParam);
        if(borrowNum>5){
            return toResponsFail("借书失败！请检查是否已超出借书上限，每人只限借5本书");
        }
        Date nowDate = new Date();
        ApiBorrow apiBorrow = new ApiBorrow();
        apiBorrow.setBookId(apiBook.getId());
        apiBorrow.setUserId(apiUser.getUserId());
        apiBorrow.setStartTime(nowDate);
        apiBorrow.setEndTime(DateUtil.getDateAfter(nowDate,10));
        apiBorrow.setStatus(4);//待借阅
        apiBorrowService.insert(apiBorrow);

        resultObject.put("apiBorrow",apiBorrow);
        return toResponsSuccess(resultObject);
    }

    /**
     * 管理员批准借书接口
     * @param borrowId
     * @return
     */
    @ResponseBody
        @RequestMapping(value="api-scan-allow-borrow")
    public String adminAllowBorrow(Integer borrowId) {
        logger.info("borrowId:"+borrowId);
        apiBorrowService.adminAllowBorrow(borrowId);
        return "1";
    }

    /**
     * 管理员批准还书接口
     * @param borrowId
     */
    @ResponseBody
    @IgnoreAuth
    @RequestMapping(value="api-scan-allow-return")
    public String adminAllowReturn(Integer borrowId,Integer delayDay) {
        logger.info("borrowId:"+borrowId);
        apiBorrowService.adminAllowReturn(borrowId,delayDay);
        return "1";
    }






}
